package servlet;

import entity.User;
import service.Impl.UserServiceImpl;
import service.UserService;
import utils.MailUtil;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.io.PrintWriter;

@WebServlet("/MailServlet")
public class MailServlet extends HttpServlet {
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doGet(request,response);
    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String opr=request.getParameter("opr");
        UserService us=new UserServiceImpl();
        PrintWriter out = response.getWriter();
        HttpSession session = request.getSession();
        if(opr.equals("send")){
            //发送邮箱验证码
            String email = request.getParameter("email");
            User user=us.selectEmail(email);
            if(user==null){
                request.setAttribute("fan","邮箱填写错误！");
                request.getRequestDispatcher("/forgetpwd.jsp").forward(request,response);
            }else{
                double rd = Math.random();
                session.setAttribute("uid",user.getId());
                String sj = String.valueOf(rd).substring(3, 7);
                //临时存储服务器，比如redis
                //存储验证码
                session.setAttribute("sj", sj);
                //去数据库检测该email是否存在
                MailUtil.sendMail(email, sj, "2y");
                response.sendRedirect("/emailveryfi.jsp");
            }
        }else if("yan".equals(opr)){
            //验证用户输入的邮箱验证码
            String sessionYzm= (String) session.getAttribute("sj");
            String userYzm=request.getParameter("yzm");
            if(sessionYzm.equals(userYzm)){
                session.removeAttribute("sj");
                request.getRequestDispatcher("/passwd.jsp").forward(request,response);
            }else {
                request.setAttribute("fan","验证码输入错误！");
                request.getRequestDispatcher("/emailveryfi.jsp").forward(request,response);
            }
        }else if("mi".equals(opr)){
            String p1=request.getParameter("p1");
            String p2=request.getParameter("p2");
            int uid=Integer.parseInt(request.getParameter("uid"));
            if(p1.equals(p2)){
                if(us.update1(p1,uid)==1){
                    session.removeAttribute("uid");
                    response.sendRedirect("/log_up.jsp");
                }else{
                    out.print("<script>alert('密码修改失败！')</script>");
                    response.sendRedirect("/index.jsp");
                }
            }else {
                request.setAttribute("fan","两次密码输入不一致，/n请重新输入！");
                request.getRequestDispatcher("/passwd.jsp").forward(request,response);
            }
        }
    }
}
